package com.weixin.tools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.weixin.sdk.api.AccessTokenApi;
import com.weixin.sdk.api.ApiResult;
import com.weixin.sdk.kit.HttpKit;
import com.weixin.sdk.msg.out.OutNewsMsg;


/**
 * 模板消息 API
 */
public class ToolTemplateMsg {
	
	private static String sendTemplateMsg = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
	private static final String MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
    private static final String UPLOADNEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=";
    private static final String MASS_SENDALL_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=";
    private static final String MASS_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=";
    private static final String MASS_DELETE_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=";
    
	/**
	 * 发送模板消息
	 */
	public static ApiResult sendTemplateMsg(String jsonStr) {
		String jsonResult = HttpKit.post(sendTemplateMsg + AccessTokenApi.getAccessToken().getAccessToken(), jsonStr);
		return new ApiResult(jsonResult);
	}
	
	/**
     * 发送客服消息
     * @param accessToken
     * @param message
     * @return
     * @
     */
    private static String sendMsg( Map<String, Object> message) {
		String reslut = HttpKit.post(MESSAGE_URL.concat(AccessTokenApi.getAccessToken().getAccessToken()), JSONObject.toJSONString(message));
		return reslut;
	}
    
    /**
     * 发送文本客服消息
     * @param openId
     * @param text
     * @ 
     */
    public static String sendText(String openId, String text)  {
        Map<String,Object> json = new HashMap<String,Object>();
        Map<String,Object> textObj = new HashMap<String,Object>();
        textObj.put("content", text);
        json.put("touser", openId);
        json.put("msgtype", "text");
        json.put("text", textObj);
    	String reslut = sendMsg( json);
        return reslut;
    }
    
    /**
     * 发送图片消息
     * @param accessToken
     * @param openId
     * @param media_id
     * @return
     * @
     */
    public static String SendImage(String openId, String media_id) {
    	Map<String,Object> json = new HashMap<String,Object>();
        Map<String,Object> textObj = new HashMap<String,Object>();
        textObj.put("media_id", media_id);
        json.put("touser", openId);
        json.put("msgtype", "image");
        json.put("image", textObj);
    	String reslut = sendMsg( json);
        return reslut;
    }
    
    /**
     * 发送语言回复
     * @param accessToken
     * @param openId
     * @param media_id
     * @return
     * @
     */
    public static String SendVoice(String openId, String media_id) {
    	Map<String,Object> json = new HashMap<String,Object>();
        Map<String,Object> textObj = new HashMap<String,Object>();
        textObj.put("media_id", media_id);
        json.put("touser", openId);
        json.put("msgtype", "voice");
        json.put("voice", textObj);
    	String reslut = sendMsg( json);
        return reslut;
    }
    
    /**
     * 发送视频回复
     * @param accessToken
     * @param openId
     * @param media_id
     * @param title
     * @param description
     * @return
     * @
     */
    public static String SendVideo(String openId, String media_id,String title,String description) {
    	Map<String,Object> json = new HashMap<String,Object>();
        Map<String,Object> textObj = new HashMap<String,Object>();
        textObj.put("media_id", media_id);
        textObj.put("title", title);
        textObj.put("description", description);
        
        json.put("touser", openId);
        json.put("msgtype", "video");
        json.put("video", textObj);
    	String reslut = sendMsg( json);
        return reslut;
    }
    
    /**
     * 发送音乐回复
     * @param accessToken
     * @param openId
     * @param musicurl
     * @param hqmusicurl
     * @param thumb_media_id
     * @param title
     * @param description
     * @return
     * @
     */
    public static String SendMusic(String openId, String musicurl,String hqmusicurl,String thumb_media_id,String title,String description) {
    	Map<String,Object> json = new HashMap<String,Object>();
        Map<String,Object> textObj = new HashMap<String,Object>();
        textObj.put("musicurl", musicurl);
        textObj.put("hqmusicurl", hqmusicurl);
        textObj.put("thumb_media_id", thumb_media_id);
        textObj.put("title", title);
        textObj.put("description", description);
        
        json.put("touser", openId);
        json.put("msgtype", "music");
        json.put("music", textObj);
    	String reslut = sendMsg( json);
        return reslut;
    }
    
    /**
     * 发送图文回复
     * @param accessToken
     * @param openId
     * @param articles
     * @return
     * @
     */
    public static String SendNews(String openId, List<OutNewsMsg> list) {
    	Map<String,Object> json = new HashMap<String,Object>();
        json.put("touser", openId);
        json.put("msgtype", "news");
        json.put("voice", list);
    	String reslut = sendMsg( json);
        return reslut;
    }
    
    /**
     * 上传图文消息素材
     * @param accessToken
     * @param articles
     * @return
     */
    public static JSONObject uploadnews(List<OutNewsMsg> list){
    	Map<String,Object> json = new HashMap<String,Object>();
    	json.put("articles", list);
    	String reslut = HttpKit.post(UPLOADNEWS_URL.concat(AccessTokenApi.getAccessToken().getAccessToken()), JSONObject.toJSONString(json));
    	if (StringUtils.isNotEmpty(reslut)) {
			return JSONObject.parseObject(reslut);
		}
		return null;
    }
    
    /**
     * 根据分组进行群发
     * @param accessToken
     * @param groupId
     * @param mpNewsMediaId
     * @return
     */
    public static JSONObject massSendall(String groupId,String mpNewsMediaId){
    	Map<String,Object> json = new HashMap<String,Object>();
    	Map<String,Object> filter = new HashMap<String,Object>();
    	Map<String,Object> mpnews = new HashMap<String,Object>();
    	filter.put("group_id", groupId);
    	mpnews.put("media_id", mpNewsMediaId);
    	
    	json.put("mpnews", mpnews);
    	json.put("filter", filter);
    	json.put("msgtype", "mpnews");
    	String reslut = HttpKit.post(MASS_SENDALL_URL.concat(AccessTokenApi.getAccessToken().getAccessToken()), JSONObject.toJSONString(json));
    	if (StringUtils.isNotEmpty(reslut)) {
			return JSONObject.parseObject(reslut);
		}
		return null;
    }
    
    /**
     * 根据OpenID列表群发
     * @param accessToken
     * @param openids
     * @param mpNewsMediaId
     * @return
     */
    public static JSONObject massSend(String openids,String mpNewsMediaId){
    	Map<String,Object> json = new HashMap<String,Object>();
    	Map<String,Object> mpnews = new HashMap<String,Object>();
    	mpnews.put("media_id", mpNewsMediaId);
    	json.put("touser", openids);
    	json.put("mpnews", mpnews);
    	json.put("msgtype", "mpnews");
    	
    	System.out.println(json.toString());
    	
    	
    	String reslut = HttpKit.post(MASS_SEND_URL.concat(AccessTokenApi.getAccessToken().getAccessToken()),json.toString());
    	
    	System.out.println(reslut);
    	
    	if (StringUtils.isNotEmpty(reslut)) {
			return JSONObject.parseObject(reslut);
		}
    	
		return null;
    }
    
    /**
     * 删除群发
     * @param accessToken
     * @param msgid
     * @return
     */
    public static JSONObject massSend(String msgid){
    	Map<String,Object> json = new HashMap<String,Object>();
    	json.put("msgid", msgid);
    	String reslut = HttpKit.post(MASS_DELETE_URL.concat(AccessTokenApi.getAccessToken().getAccessToken()), JSONObject.toJSONString(json));
    	if (StringUtils.isNotEmpty(reslut)) {
			return JSONObject.parseObject(reslut);
		}
		return null;
    }
    
    public static void main(String[] args) {
		
	}
//	//给用户推送消息
//	logger.info(WeChat.message.sendText(accessToken , openid , "测试微信开发"));
//	
//	//发送图文
//	logger.info(WeChat.message.SendImage(accessToken, openid, "JT6sjLDVHGrc5EjxH2LwWcOMp94tHMUUUw1kNqq5b-BjflB593_KZOhX4vk3825R"));
//
//	//发送语音
//	logger.info(WeChat.message.SendVoice(accessToken, openid, "uUoq5BBmeDDN5IEL__GeV9a1R9gijc-yJn92ia4rjiVDV0-y3cDOgSZ1yZutosfl"));
//	
//	//发送视频
//	logger.info(WeChat.message.SendVideo(accessToken, openid, "9cxptX-mNd3F0iCnScDxFn0Hah2XYMW2EQgXaoAl9jGuZeeZrkbSak4lDHs1BexU","视频","发送视频啦"));
//	
//	//发送音乐
//	logger.info(WeChat.message.SendMusic(accessToken, openid, "http://qqmusic.xbdj.cn/music/6/38151791.mp3","http://fm.baidu.com","uUoq5BBmeDDN5IEL__GeV9a1R9gijc-yJn92ia4rjiVDV0-y3cDOgSZ1yZutosfl","盛夏晚晴天 如果爱老了","盛夏晚晴天 如果爱老了"));
//
}